package com.util.email;

import java.io.IOException;
import java.net.UnknownHostException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.entity.User;

public class MailConfirmUtils implements ErrorMailInterface {

    private String mail_smtp_host;

    private String username;

    private String password;

    public List<String> addressee = new ArrayList<String>();

    private String message;

    private String title;

    private String fromAddress;

    public MailConfirmUtils() throws UnknownHostException {
        readEmailXml("classpath:email.xml");
        this.message = "恭喜";
    }
    
    public MailConfirmUtils(User user) {
        readEmailXml("classpath:email.xml");
        this.addressee = getListFromList(user.getEmail());
        DesUtils des ;
        try {
            des = new DesUtils();
    		DateFormat dFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            java.util.Date date=new java.util.Date();
            String dateString= dFormat.format(date).toString();
           // dateString=des.encrypt(dateString);
            this.message = "<html>点击以下链接激活</br><a target='_blank' href='http://localhost:8080/miniRenRen/login/login_toConfirm.action?data="+des.encrypt(user.getEmail()+"&"+user.getPassword()+"&"+dateString)+" '>"+des.encrypt(user.getEmail()+"&"+user.getPassword()+"&"+dateString)+"</html>";
        } catch (Exception e) {
            System.out.println("Mail加密错误");
            e.printStackTrace();
        }
    }
    
   
    /**
     * 从xml中读取email信息
     */
    private void readEmailXml(String xmlPath) {
        try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
                    IOUtil.getFile(xmlPath));
            doc.getDocumentElement().normalize();
            NodeList listOfEmail = doc.getElementsByTagName("email");
            Element email = (Element) (listOfEmail.item(0));

            this.mail_smtp_host = getNodeValue(email, "mail_smtp_host");
            this.username = getNodeValue(email, "username");
            this.password = getNodeValue(email, "password");
            this.title = getNodeValue(email, "title");
            this.fromAddress = getNodeValue(email, "fromAddress");

        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获得该节点的值
     * 
     * @param eName
     * @param nodeName
     * @return
     */
    private String getNodeValue(Element eName, String nodeName) {
        NodeList list = eName.getElementsByTagName(nodeName);
        Node node = list.item(0);
        NodeList textList = node.getChildNodes();
        return ((Node) textList.item(0)).getNodeValue().trim();
    }

    /**
     * 按照分号切分发送email地址
     * 
     * @param string
     * @return
     */
    public List<String> getListFromList(String string) {
        List<String> addressList = new ArrayList<String>();
        String[] path = string.split(";");
        for (int i = 0; i < path.length; i++) {
            if (path[i] == "") continue;
            addressList.add(path[i]);
        }

        return addressList;
    }

    public String getMail_smtp_host() {
        return mail_smtp_host;
    }

    public void setMail_smtp_host(String mailSmtpHost) {
        mail_smtp_host = mailSmtpHost;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<String> getAddressee() {
        return addressee;
    }

    public void setAddressee(List<String> addressee) {
        this.addressee = addressee;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }
}
